<?php
########################################################
# DMS Donations Management System v1.0                 #
# created by:  Isaac Sabas                             #
#			   Nikko Reyes							   #
#			   Miko Tiamwatt						   #
#                                                      #
# De La Salle Philippines                              #
# All Rights Reserved DLSP Copyright 2009              #
########################################################

define('ACCESS', true);	

session_start();	
	
# set error checking levels #	
	# enable these lines when site is to be deployed #	
	ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    //ini_set('log_errors', 'On');
    //ini_set('error_log', '/usr/local/apache/logs/error_log');
	
	#############################
	# include some needed files #
	#############################
		$absRoot = '../../';
		include($absRoot . 'lib/XAJAX/xajax_core/xajax.inc.php');
		include($absRoot . 'src/ClientHandle.php');

	$id = $_GET["client_id"];

	$xajax = new xajax();

	$xajax->registerFunction("addClientPhone");

	$xajax->registerFunction("update3rd");
	
	function update3rd ($type) {
		$statusresponse = new xajaxResponse();
	
	if($type == 'Mobile')
	$statusresponse->assign('3rd', 'innerHTML', 'Provider:');
	else
	if($type == 'Home' || $type == 'Office')
	$statusresponse->assign('3rd', 'innerHTML', 'Area Code:');
	
	return$statusresponse;
	}

	
		function addClientPhone($type, $priority, $countrycode, $areaprovider, $number, $id) {
		$statusresponse = new xajaxResponse();
	
		# check if the fields are empty
			if (empty($type) || empty($priority) || empty($countrycode) || empty($areaprovider) || empty($number) || empty($id)) {
				$statusresponse->assign('Status','style.color','red');
				$statusresponse->assign('Status', 'innerHTML', 'Please complete all fields.<br /> Not updating..');
			}
			else {

			if ($id != '') {
				
			$client = new ClientHandle();	
			
			if($type == 'Mobile')
			{
			
			$Mobile['mobile_countrycode']=$countrycode;
			$Mobile['mobile_provider']=$areaprovider;
			$Mobile['mobile_number']=$number;
			$Mobile['mobile_priority']=$priority;
			
			$result=$client->addNewMobileNumber($Mobile,$id);	

			}
			
			else if($type == 'Office' || $type == 'Home')
			{
			
			$Phone['phone_countrycode']=$countrycode;
			$Phone['phone_areacode']=$areaprovider;
			$Phone['phone_number']=$number;
			$Phone['phone_type']=$type;
			$Phone['phone_priority']=$priority;
			
			$result=$client->addNewPhoneNumber($Phone,$id);
			
			}
			
						if($result == true)
						{
							$statusresponse->call("parent.window.frames['view_client_profile_".$id."'].location.reload()"); 
							$statusresponse->assign('Status','style.color','green');
							$statusresponse->assign('Status', 'innerHTML', 'Client Number added.<br /> Database updated.');
						}
						else
						{
							$statusresponse->assign('Status','style.color','red');
							$statusresponse->assign('Status', 'innerHTML', 'Database error.<br />Not updating..');
						}
			
					}
					
			}
								
		return$statusresponse;
	}
	
	$xajax->processRequest();		
	
# instantiate predifined classes #											

	//print_r($result);
?>

<html>
<head>
	<script type="text/javascript" src="../../lib/XAJAX/xajax_js/xajax_core.js" />
	<link rel="stylesheet" type="text/css" href="../../lib/YUI/yui/build/fonts/fonts-min.css" />
	<?php $xajax->printJavaScript(); ?>	
</head>
<body>											

<form id="EditClientAddresses" action="#" method='post'>
<table>
	<tr>
	<td>
	Phone Type:</td>
	<td>
			<select id="Type" name="Type" onchange="xajax_update3rd(document.getElementById('Type').value)">
			<option id="Home" value="Home">Home</option>
			<option id="Office" value="Office">Office</option>
			<option id="Mobile" value="Mobile">Mobile</option>
		</select>
	</td>
	</tr>
	<tr>
	<td>
	Priority:</td>
	<td>
		<input type=text id="Priority" name="Priority"></input>
	</td>
	</tr>
	<tr>
	<td>
	Country Code:</td>
	<td>
		<input type=text id="CountryCode"></input>
	</td>
	</tr>
	<tr>
	<td id="3rd">
	Area Code:</td>
	<td>
	<input type=text id="AreaProvider"></input>
	</td>
	</tr>
	<tr>
	<td>
	Number:</td>
	<td>
	<input type=text id="Number"></input>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	<td>
	<input type=button id="Update" value="Update" onClick="xajax_addClientPhone(document.getElementById('Type').value,document.getElementById('Priority').value, document.getElementById('CountryCode').value,document.getElementById('AreaProvider').value,document.getElementById('Number').value,<?php echo $id;?>);"></input>
	<input type=reset id="Reset" value="Reset"></input>
	</td>
	</tr>
	
</table>
</form>

<div id='Status'></div>

</body>

</html>